package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.system.domain.SysSmsset;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.mapper.SysSmssetMapper;
import com.ruoyi.system.service.SendSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class SendSmsServiceImp implements SendSmsService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SendSmsServiceImp.class);

    //采用注入的方式传递参数
    @Value("${aliyun.accessKeyID}")
    private String accessKeyID;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Autowired
    private SysSmssetMapper sysSmssetMapper;

    @Override
    public boolean sendSms(String phoneNum, String code) {
        SysSmsset sysSmsset = sysSmssetMapper.selectSysSmssetList(new SysSmsset()).get(0);
        DefaultProfile profile=DefaultProfile.getProfile("cn-hangzhou", sysSmsset.getKeyid(),sysSmsset.getKeysecret());
        IAcsClient client=new DefaultAcsClient(profile);
        CommonRequest request=new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", sysSmsset.getSignname1());
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("TemplateCode", sysSmsset.getTemplate1());

        Map<String,Object> param=new HashMap<>();
        param.put("code", code);

        request.putQueryParameter("TemplateParam", JSON.toJSONString(param));

        try {
            CommonResponse response=client.getCommonResponse(request);
            //System.out.println(response.getData());//返回的消息
            LOGGER.info(JSON.parseObject(response.getData(), Map.class).get("Message").toString());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
    @Override
    public boolean sendSms_1(String phoneNum, String code) {
        SysSmsset sysSmsset = sysSmssetMapper.selectSysSmssetList(new SysSmsset()).get(0);
        DefaultProfile profile=DefaultProfile.getProfile("cn-hangzhou", sysSmsset.getKeyid(),sysSmsset.getKeysecret());
        IAcsClient client=new DefaultAcsClient(profile);
        CommonRequest request=new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", sysSmsset.getSignname2());
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("TemplateCode", sysSmsset.getTemplate2());

        Map<String,Object> param=new HashMap<>();
        param.put("code", code);

        request.putQueryParameter("TemplateParam", JSON.toJSONString(param));

        try {
            CommonResponse response=client.getCommonResponse(request);
            //System.out.println(response.getData());//返回的消息
            LOGGER.info(JSON.parseObject(response.getData(), Map.class).get("Message").toString());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
}

